package com.example.servlet.ClassServlet;

import com.example.bean.Class;
import com.example.dao.ClassDao.ClassDeleteDao;
import com.example.dao.ClassDao.ClassDeleteDaoImp;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "ClassDeleteServlet", value = "/classdelete")
public class ClassDeleteServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取需要删除的班级的名称
        request.setCharacterEncoding("utf-8");
        String cname = request.getParameter("cname");
        System.out.println("需要删除的班级的名称为:"+cname);

        //2.把获取到的信息通过Dao层处理
        Class cl=new Class(cname);
        ClassDeleteDao classDeleteDao=new ClassDeleteDaoImp();
        int res = classDeleteDao.getClassDelete(cl);

        response.setContentType("text/html;charset=utf-8");
        //3.若删除成功 返回成功
        if(res==1){
            System.out.println("删除班级信息成功!");

            request.getSession().setAttribute("success",cname);
            response.sendRedirect("classdeletesuccess.jsp");
        }else{
            System.out.println("删除班级信息失败!");
            request.setAttribute("fail","删除班级信息失败!");
            request.getRequestDispatcher("classdelete.jsp").forward(request,response);
        }
    }
}
